package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.model.UserPageInfoModel;
import com.hangzhou.xyj.net.handler.GetFollowUsersHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetFollowUsersReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.MyFocusAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.constants.Constants;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.List;

public class HisFocusActivity extends BaseActivity {
    private RecyclerView mRvFocus;
    private MyFocusAdapter mAdapter;
    private String mTitle;//标题
    private String mUserId;//用户id
    private MyTextView mTvNobody;//没有的提示

    @Override
    public void initIntentData(Intent intent) {
        mTitle = intent.getStringExtra(DataKey.TITLE);
        mUserId = intent.getStringExtra(DataKey.INDEX);
    }

    @Override
    public void initTop() {
        if (StringUtil.isNotEmpty(mTitle)) {
            enableTitle(true, mTitle);
        } else {
            enableTitle(true, "我的关注");
        }
        enableRefresh(true);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_his_focus);
    }

    @Override
    public void initViews() {
        mRvFocus = findView(R.id.rv_my_focus);
        mTvNobody = findView(R.id.mtv_nobody);
        mRvFocus.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
        mRvFocus.setItemAnimator(new DefaultItemAnimator());
        mRvFocus.setAdapter(mAdapter = new MyFocusAdapter(mRvFocus, null));
    }

    @Override
    public void initDatas() {
        getData(1);
    }

    @Override
    public void initListeners() {
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRvFocus.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getData(mAdapter.getCurrentPage());
                    }
                }, 800);
            }
        });
    }

    /**
     * 发送请求并获取数据
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/17 10:03
     */
    private void getData(final int currentPage) {
        GetFollowUsersReq getFollowUsersReq;
        if (StringUtil.isNotEmpty(mUserId)) {
            getFollowUsersReq = new GetFollowUsersReq(Integer.valueOf(mUserId), Constants.PAGE_SIZE, currentPage);
        } else {
            getFollowUsersReq = new GetFollowUsersReq(mMyUserId, Constants.PAGE_SIZE, currentPage);
        }
        //请求数据
        HttpLoader.getInstance().sendRequest(new GetFollowUsersHandler(getFollowUsersReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<UserModel> userList = ((UserPageInfoModel) model).getUserList();
                if (currentPage == 1 && (userList == null || userList.isEmpty())) {
                    setNoDataTip(true, "用户暂无关注任何人");
                } else if (currentPage == 1) {
                    if (userList.size() > 0) {
                        mTvNobody.setVisibility(View.GONE);
                    }
                    mAdapter.load(userList);
                } else {
                    mAdapter.update(userList);
                }
            }

            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }

}
